package com.talkyun.utils.subnet;

import com.talkyun.utils.subnet.SubnetUtils.SubnetInfo;

/**
 * Some ip address helper
 * <p>
 * Created by chenbin on 2017/1/20.
 */

public class AddressUtils {

    // TODO cache subnet-utils
    public static boolean isInRange(String ip, String cidr) {
        SubnetUtils su = new SubnetUtils(cidr);
        return su.getInfo().isInRange(ip);
    }

    public static SubnetInfo getSubnetInfo(String cidr) {
        SubnetUtils su = new SubnetUtils(cidr);
        return su.getInfo();
    }

    public static void main(String[] args) {
        SubnetInfo subnet = AddressUtils.getSubnetInfo("192.168.1.0/24");
        System.out.println(subnet.getAddress());
        System.out.println(subnet.getAddressCountLong());
        System.out.println(subnet.getNetmask());
        System.out.println(subnet.getCidrSignature());

        long time = System.currentTimeMillis();
        for (int i = 0; i < 100 * 10000; i++) {
            AddressUtils.isInRange("192.168.1.6", "192.168.1.0/24");
            AddressUtils.isInRange("192.168.1.3", "10.168.1.0/24");
            AddressUtils.isInRange("10.10.1.3", "10.10.0.0/16");
        }

        System.out.println(System.currentTimeMillis() - time);
    }
}